#!/usr/bin/python

# Copyright (c) 2009, Purdue University
# All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 
# Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# Redistributions in binary form must reproduce the above copyright notice, this
# list of conditions and the following disclaimer in the documentation and/or
# other materials provided with the distribution.
# 
# Neither the name of the Purdue University nor the names of its contributors
# may be used to endorse or promote products derived from this software without
# specific prior written permission.
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

"""Update named tool for Roster"""


__copyright__ = 'Copyright (C) 2009, Purdue University'
__license__ = 'BSD'
__version__ = '0.12'


import os
import sys
import getpass
import datetime

from optparse import OptionParser

from roster_user_tools import cli_record_lib
from roster_user_tools import cli_common_lib
from roster_user_tools import roster_client_lib


def main(args):
  """Collects command line arguments, checks ip addresses and adds records.

  Inputs:
    args: list of arguments from the command line
  """
  parser = OptionParser(version='%%prog (Roster %s)' % __version__)

  parser.add_option('-f', '--file', action='store', dest='file',
                    help='File name of hosts file to write to database.',
                    metavar='<file-name>', default='roster_named')
  parser.add_option('-d', '--dns-server-set', action='store',
                    dest='dns_server_set',
                    help='String of the dns server set name.',
                    metavar='<dns-server-set>', default=None)
  parser.add_option('-l', '--list', action='store_true', dest='list',
                    help='List all revisions for a particular dns server set.',
                    default=False)
  parser.add_option('-n', '--newest', action='store_true', dest='newest',
                    help='Write the newest configurationo to a file given '
                         'DNS server set.', default=False)
  parser.add_option('-e', '--edit', action='store_true', dest='edit',
                    help='Edit a particular revision.',
                    default=False)
  parser.add_option('-i', '--option-id', action='store', dest='option_id',
                    help='Integer of option id.', metavar='<option-id>',
                    default=None)
  parser.add_option('-r', '--revert', action='store', dest='revert',
                    help='Integer of option id to revert.',
                    metavar='<option-id>', default=None)
  parser.add_option('-t', '--timestamp', action='store', dest='timestamp',
                    help='String of timestamp in YYYY/MM/DD/HH/MM/SS format.',
                    metavar='<timestamp>', default=None)
  parser.add_option('--update', action='store_true', dest='update',
                    help='Update changes to file to database.',
                    default=False)
  parser.add_option('-s', '--server', action='store', dest='server',
                    help='XML-RPC Server address.', metavar='<server>',
                    default=None)
  parser.add_option('--config-file', action='store', dest='config_file',
                    help='Config file location.', metavar='<file>',
                    default=None)
  parser.add_option('-u', '--username', action='store', dest='username',
                    help='Run as a different username.', metavar='<username>',
                    default=unicode(getpass.getuser()))
  parser.add_option('-p', '--password', action='store', dest='password',
                    help='Password string, NOTE: It is insecure to use this '
                    'flag on the command line.', metavar='<password>',
                    default=None)
  parser.add_option('-c', '--cred-file', action='store', dest='credfile',
                    help='Location of credential file.', metavar='<cred-file>',
                    default=None)
  parser.add_option('-q', '--quiet', action='store_true', dest='quiet',
                    help='Suppress program output.', default=False)


  (globals()["options"], args) = parser.parse_args(args)

  try:
    cli_common_lib_instance = cli_common_lib.CliCommonLib(options)
  except cli_common_lib.ArgumentError, e:
    print 'ERROR: %s' % e
    sys.exit(1)
  cli_record_lib_instance = cli_record_lib.CliRecordLib(cli_common_lib_instance)

  timestamp = options.timestamp
  if( options.option_id is not None ):
    options.option_id = int(options.option_id)
  if( options.timestamp is not None ):
    try:
      timestamp = datetime.datetime.strptime(options.timestamp, '%Y-%m-%d %H:%M:%S')
    except ValueError:
      cli_common_lib_instance.DnsError('Timestamp incorrectly formatted.', 1)

  if( options.newest ):
    cli_common_lib_instance.DisallowFlags(['list', 'edit', 'option_id',
                             'timestamp', 'revert', 'update'],
                             parser)
    if( options.dns_server_set is None ):
      cli_common_lib_instance.DnsError('Must specify a dns server set with -d.', 1)
    named_option = roster_client_lib.RunFunction(
        u'ListLatestNamedConfig', options.username,
        credfile=options.credfile, args=[options.dns_server_set],
        server_name=options.server)['core_return']
    handle = open(options.file, 'w')
    try:
      handle.writelines(named_option['options'])
    finally:
      handle.close()
    print 'Wrote file: %s' % options.file
  elif( options.update ):
    cli_common_lib_instance.DisallowFlags(['list', 'edit', 'option_id',
                                  'timestamp', 'revert'],
                                 parser)
    handle = open(options.file, 'r')
    try:
      named_file = handle.read()
    finally:
      handle.close()
    roster_client_lib.RunFunction(u'MakeNamedConfGlobalOption',
                                  options.username,
                                  credfile=options.credfile,
                                  args=[options.dns_server_set,
                                        named_file],
                                  server_name=options.server)
    if( not options.quiet ):
      print 'ADDED NAMED_CONF_GLOBAL_OPTION: %s' % options.file

  elif( options.list ):
    cli_common_lib_instance.DisallowFlags(['revert', 'edit', 'file'],
                                 parser)
    named_options = roster_client_lib.RunFunction(
        u'ListNamedConfGlobalOptionsClient', options.username,
        credfile=options.credfile, kwargs={'option_id': options.option_id,
                                           'dns_server_set':
                                               options.dns_server_set,
                                           'timestamp': timestamp},
        server_name=options.server)['core_return']
    print_list = []
    for option in named_options:
      print_list.append([option['id'],
        datetime.datetime.strptime(option['timestamp'].value,
                                   "%Y%m%dT%H:%M:%S").strftime(
                                       '%Y-%m-%d %H:%M:%S'),
        option['dns_server_set_name']])
    print cli_common_lib_instance.PrintColumns(print_list)
  elif( options.edit ):
    cli_common_lib_instance.DisallowFlags(['revert'], parser)
    if( not options.option_id and not options.dns_server_set
        and not options.timestamp):
      cli_common_lib_instance.DnsError('Either an option id or dns server set and '
                              'timestamp are needed.', 1)
    named_options = roster_client_lib.RunFunction(
        u'ListNamedConfGlobalOptionsClient', options.username,
        credfile=options.credfile, kwargs={'option_id': options.option_id,
                                           'dns_server_set':
                                               options.dns_server_set,
                                           'timestamp': timestamp},
        server_name=options.server)['core_return']
    if( len(named_options) == 0 ):
      cli_common_lib_instance.DnsError('No configurations found.', 1)
    elif( len(named_options) == 1 ):
      handle = open(options.file, 'w')
      try:
        handle.writelines(named_options[0]['options'])
      finally:
        handle.close()
      print 'Wrote file: %s' % options.file
    else:
      cli_common_lib_instance.DnsError('Multiple configurations found. This could be '
                              'due to an internal error or arguments may be '
                              'too general.', 1)
  elif( options.revert ):
    cli_common_lib_instance.DisallowFlags(['file'], parser)
    if( not options.option_id ):
      cli_common_lib_instance.DnsError('To revert a configuration, the desired '
                              'replacement must be specified with -i', 1)
    roster_client_lib.RunFunction(u'RevertNamedConfig', options.username,
        credfile=options.credfile, args=[options.revert,
                                         int(options.option_id)],
        server_name=options.server)
    if( not options.quiet ):
      print 'REVERTED NAMED_CONF_GLOBAL_OPTION: dns_server_set: %s rev: %s' % (
          options.revert, options.option_id)
  else:
    parser.print_help()
    cli_common_lib_instance.DnsError('An ip address/range/file/update must be '
                            'specified.', 1)

if __name__ == "__main__":
    main(sys.argv[1:])
